package com.example.evanproject.controller;

import com.example.evanproject.service.impl.TrendsServiceImpl;
import com.example.evanproject.util.AjaxResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("trends")
public class TrendsController {

    @Autowired
    private TrendsServiceImpl trendsService;

//    返回个人的全部动态详情
    @RequestMapping("selectbyuserid")
    public AjaxResult selectbyuerid(Integer userid){

        return AjaxResult.success(trendsService.filterbigpeople(userid)) ;
    }
//       //返回某个动态的详细信息
    @RequestMapping("slectbyid")
    public  AjaxResult selectbyidinfo(Integer id){

        return AjaxResult.success(trendsService.selectbyidinfo(id)) ;
    }

//    返回所有的发布动态
@RequestMapping("selectall")
public AjaxResult selectall(){
        return AjaxResult.success(trendsService.selectall()) ;
}


//    发布动态
@PostMapping("updatelist")
public AjaxResult updatelistinfo(String text, Integer userid, HttpServletRequest request) throws ServletException, IOException {

    System.out.println("99999");
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Iterator<String> fileNames = multipartRequest.getFileNames();
    if (fileNames.hasNext()) {
        List<MultipartFile> fileList = multipartRequest.getFiles("file");
        if (fileList.size() > 0) {
            //遍历文件列表
            MultipartFile[] filelist= fileList.toArray(new MultipartFile[0]);
            System.out.println(filelist);
            return AjaxResult.success(trendsService.updatelistinfo(filelist,text,userid)) ;
        }

    }
    return AjaxResult.success(trendsService.updatelistinfo(null,text,userid)) ;

//    return ;
}

    //    发布评论
    @RequestMapping("updatecomment")
    public AjaxResult updatecomment(Integer userid,Integer fid,String text){

       return AjaxResult.success(trendsService.updatecomment(userid,fid,text)) ;

    }

//    删除动态
    @RequestMapping("deleteinfobyid")
    public AjaxResult deletetrends(Integer treandid){
        return AjaxResult.success(trendsService.deletetrends(treandid)) ;
    }

}
